﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Xml;
using FrameWork.Classes.Data;
using FrameWork.Event;
using FrameWork.InterFace;

namespace FrameWork.Controls
{
    public class DirectUITemplate : ControlContainer, ILayoutPanel
    {
        public DirectUITemplate()
            : base()
        {
        }

        public DirectUITemplate(int x, int y, int width, int height)
            : base(x, y, width, height)
        {
        }

        protected override void InitControl()
        {
            base.InitControl();
            BackColor = Color.Empty;
            NeedDataBind = false;
        }

        /// <summary>
        /// Gets or sets the index, for instance the item index if this is a template for a listbox.
        /// </summary>
        public int ItemIndex { get; set; }

        private object item;

        /// <summary>
        /// Gets or sets the item for the template.
        /// </summary>
        public object Item
        {
            get { return item; }
            set
            {
               // if (item != value)
               // {
                    item = value;
                    OnBindValue(value);
                //}
                OnItemUpdate(value);
            }
        }

        protected virtual void OnItemUpdate(object value)
        {
        }

        /// <summary>
        /// Occurs when the template must be data bound.
        /// </summary>
        protected virtual void OnBindValue(object value)
        {
            if (BindValue != null) BindValue(this, EventArgs.Empty);
            foreach (DirectUIControl c in this.Controls)
            {
                c.NeedDataBind = true;
                if (c.BindingCollection != null)
                {
                    foreach (DataBinding d in c.BindingCollection)
                    {
                        d.Mode = BindingMode.OneWay;
                        DataBinder.SetPropertyValue(c, d.PropertyName, GetDataSourceValue(value,d));

                    }
                }
            }
        }
        private object GetDataSourceValue(object dataSource, DataBinding binding)
        {
                        object returnValue = dataSource;
           if (dataSource is XmlNode)
                {
                if (!string.IsNullOrEmpty(binding.XPath))
                {
                    //   returnValue = ((System.Xml.XmlDocument)((XmlDataProvider)dataSource).Data).SelectSingleNode(binding.XPath);
                    returnValue = ((System.Xml.XmlNode)dataSource).SelectNodes(binding.XPath);
                    if (((XmlNodeList)returnValue).Count <= 0)
                    {
                        throw new Exception.DataBinderException("Could Not Find XML Path " + binding.XPath);
                    }
                }
            }
           
            else
            {
                if (binding.Path != null)
                {
                    returnValue = DataBinder.GetPropertyValue(dataSource, binding.Path.Path);
                }
            }
           
            return returnValue;
        }
        /// <summary>
        /// Occurs when the template must be data bound.
        /// </summary>
        public event EventHandler BindValue;

        public override void RaiseCommand(CommandEventArgs e)
        {
            e.Index = ItemIndex;
            if (e.Data == null) e.Data = Item;
            base.RaiseCommand(e);
        }

        /// <summary>
        /// Binds the data for the template. Before accessing data outside a OnBind event, you must call this method, otherwise ItemIndex and Item might
        /// return false data since the template is reused and bound on occasion.
        /// </summary>
        public void Bind()
        {
            ITemplateHost host = Container as ITemplateHost;
            if (host != null) host.Bind(this);
        }

        /// <summary>
        /// Binds this template with a specified item.
        /// </summary>
        /// <param name="itemIndex">The index of the item for which to bind.</param>
        public void Bind(int itemIndex)
        {
            ITemplateHost host = Container as ITemplateHost;
            if (host != null) host.Bind(this, itemIndex);
        }

  

        //     public override bool Selectable { get { return true; } }


    }
}
